package com.pybox.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.pybox.common.utils.JdbcTemplateUtils;
import com.pybox.common.utils.ParamUtils;
import com.pybox.dto.TCanbinetEventLogsDto;
import com.pybox.entity.TServerLogs;
import com.pybox.repository.TServiceRepository;
import com.pybox.util.FileUtils;
import com.pybox.util.HttpSessionUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LeiYang
 * @title: TServerLogService
 * @projectName pybox
 * @description: TODO
 * @date 2019/8/817:45
 */
@Service
@Transactional

public class TServerLogService {

    @Autowired
    private TServiceRepository tServiceRepository;
    @Autowired
    private JdbcTemplateUtils jdbcTemplateUtils;

    //保存日志
    public void save(TServerLogs sysLog) {
        tServiceRepository.save(sysLog);
    }

    /**
     * 分页查询查询TCanbinetServerEventLogs（服务端操作日志）
     *
     * @param pageNo
     * @param pageSize
     * @param paramMap
     * @return
     */
    public Page<TServerLogs> pageQuery(Integer pageNo, Integer pageSize, Map<String, String> paramMap) {

        Pageable pageable = new PageRequest(pageNo - 1, pageSize);
        List<Object> object = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT id,operateclass,operatemethod,ipds,operateparams, operate,user_id,realname as userName,ctime FROM `t_server_logs` where 1 =1 ");
        String where = new ParamUtils().getParams(paramMap, sql);
        Page<TServerLogs> data = jdbcTemplateUtils.page(where, object, TCanbinetEventLogsDto.class, pageNo, pageSize);
        List<TServerLogs> list = data.getContent();
        Page<TServerLogs> page = new PageImpl<>(list, pageable, data.getTotalElements());
        return page;
    }


    public void exportExcel(Map<String, String> paramsMap, HttpServletResponse response) throws IOException {
        List<Object> object = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT id,operateclass,operatemethod,ipds,operateparams, operate,user_id,realname as userName,ctime FROM `t_server_logs` where 1 =1 ");
        String where = new ParamUtils().getParams(paramsMap, sql);
        List<TServerLogs> list = jdbcTemplateUtils.list(where,object,TServerLogs.class);
        //指定列表标题和工作表名称
        ExportParams params = new ExportParams("服务端日志表", "服务端日志表");
        Workbook workbook = ExcelExportUtil.exportExcel(params, TServerLogs.class, list);

        //下载导出 设置头信息
        String filename = "服务端日志表.xls";
        String agent = HttpSessionUtil.getRequest().getHeader("user-agent");
        filename = FileUtils.encodeDownloadFilename(filename,agent);
        response.setHeader("content-Disposition", "attachment;filename=" + filename);

        workbook.write(response.getOutputStream());
        workbook.close();
    }
}
